Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getPort to resolve Port(0) #17559

Merged
merged 4 commits into from
Mar 30, 2021
Merged

add getPort to resolve Port(0) #17559

merged 4 commits into from
Mar 30, 2021

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Mar 28, 2021

I also updated the module-level example to promote best practices and avoid issues like Address already in use caused by hard coded ports as explained in #14327

future work

@timotheecour timotheecour added the TODO: followup needed remove tag once fixed or tracked elsewhere label Mar 28, 2021
@timotheecour timotheecour removed the TODO: followup needed remove tag once fixed or tracked elsewhere label Mar 28, 2021
@timotheecour timotheecour requested a review from ringabout March 29, 2021 00:17
Comment on lines 89 to 99
func getSocket*(self: AsyncHttpServer): AsyncSocket {.since: (1, 5, 1).} =
## Field accessor.
runnableExamples:
from std/asyncdispatch import Port
from std/asyncnet import getFd
from std/nativesockets import getLocalAddr, AF_INET
let server = newAsyncHttpServer()
server.listen(Port(0)) # Socket is not bound until this point
let port = getLocalAddr(server.getSocket.getFd, AF_INET)[1]
doAssert uint16(port) > 0
# note: a more direct way to get the port is `getPort`.
let (laddr, port) = getLocalAddr(server.getSocket.getFd, AF_INET)
assert uint16(port) > 0
assert laddr == "0.0.0.0"
server.close()
a.socket
self.socket
Copy link
Contributor

@dom96 dom96 Mar 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like there isn't much reason to keep this now that we have getPort, I don't see another reason why it would be useful. Since it was introduced in the same version it shouldn't cause breakage, can we remove it in this PR?

Copy link
Member Author

@timotheecour timotheecour Mar 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe but it belongs in a separate PR, I've added a future work item to address this. getPort doesn't subsume getSocket, so that should be discussed separately. eg, it shows you can use this to get assert laddr == "0.0.0.0" in this example and there may be other use cases for getSocket.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, I don't see a reason not to do this in the same PR. But here you go: #17587

@timotheecour timotheecour added the TODO: followup needed remove tag once fixed or tracked elsewhere label Mar 29, 2021
lib/pure/asynchttpserver.nim Outdated Show resolved Hide resolved
@timotheecour
Copy link
Member Author

PTAL

@Araq Araq merged commit 7298850 into nim-lang:devel Mar 30, 2021
@timotheecour timotheecour deleted the pr_getPort branch March 30, 2021 19:27
@timotheecour timotheecour removed the TODO: followup needed remove tag once fixed or tracked elsewhere label Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants